﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ccit.ClassLibrary;
using Ccit.Utils;
using Newtonsoft.Json;

namespace Ccit.SysPlat.Organ
{
    /// <summary>
    /// Description: 系统平台 -> 组织机构 -> 用户信息维护
    /// Author: tim 2010-02-10
    /// </summary>
    public partial class sys_user_edit : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _hidOrganId.Value = (null != Request.QueryString["organId"]) ? Request.QueryString["organId"].ToString() : "1";
                _hidUserId.Value = (null != Request.QueryString["userId"]) ? Request.QueryString["userId"].ToString() : string.Empty;
                /* 页面嵌入标志: 0:非嵌入 1:嵌入式 */
                _hidInsetSign.Value = (null != Request.QueryString["inset"]) ? Request.QueryString["inset"].ToString() : "0";               
                initializePage();
            }
        }

        private void initializePage()
        {
            initSysSwitch(); //初始化系统开关
            eEffect ukEffect = (eEffect)Enum.Parse(typeof(eEffect), _hidSysUK.Value);
            eEffect multiDeptEffect = (eEffect)Enum.Parse(typeof(eEffect), _hidSysMultiDept.Value);
            initAddNew(ukEffect,multiDeptEffect);

            UserBLL userBLL = new UserBLL();
            string userId = _hidUserId.Value;
            MsUser msUser = userBLL.GetUserInfo(userId,eTrueFalse.TRUE);
            initEdit(msUser, ukEffect, multiDeptEffect);

            msUser = null;
            userBLL.Release();
            userBLL = null;
        }


        private void initAddNew(eEffect ukEffect,eEffect multiDeptEffect)
        {            
            string organId = _hidOrganId.Value;
            

            txtUserName.Text = string.Empty;
            txtUserCode.Text = string.Empty;
            txtLgName.Text = string.Empty;

            #region 初始化级别下拉控件
            ClsBLL clsBLL = new ClsBLL();
            List<MsKeyValue> lstCls = clsBLL.GetClsListByOrgan(organId, string.Empty);
            CtrInterface.BindDropDown(ref ddlClass, lstCls, false, string.Empty);
            lstCls = null;
            clsBLL.Release();
            clsBLL = null;
            #endregion

            #region 初始化身份认证相关控件
            if (ukEffect == eEffect.NO)
            {
                rdlist.SelectedValue = "0";
                rdlist.Enabled = false;
                txtUseKey.Enabled = false;
            }
            else
            {
                rdlist.SelectedValue = "0";
                rdlist.Enabled = true;
                txtUseKey.Enabled = true;
            }
            #endregion

            #region 初始化部门、职位相关控件
            if (multiDeptEffect == eEffect.NO)
            {
                trSingle.Visible = true;
                trMulti.Visible = false;

                DeptBLL deptBLL = new DeptBLL();
                List<MsKeyValue> lstDept = deptBLL.GetALLDepts(organId, string.Empty);
                CtrInterface.BindDropDown(ref ddlDept, lstDept, false, string.Empty);

                PosBLL posBLL = new PosBLL();
                List<MsKeyValue> lstPos = posBLL.GetPosListByOrgan(organId, string.Empty);
                CtrInterface.BindDropDown(ref ddlPos, lstPos, false, string.Empty);

                lstDept = null;
                lstPos = null;
                deptBLL.Release();
                posBLL.Release();
                deptBLL = null;
                posBLL = null;
            }
            else {
                trSingle.Visible = false;
                trMulti.Visible = true;            
            }

            #endregion
        }

        /// <summary>
        /// 初始化系统开关设置
        /// </summary>
        private void initSysSwitch()
        {
            string effect = ConfigLib.GetAttribute(eNodeType.INDENTIFYUSER);
            _hidSysUK.Value = (string.IsNullOrEmpty(effect)) ? "0" : effect; //系统启用身份认证功能开关

            string multiDept = ConfigLib.GetAttribute(eNodeType.MULTIDEPT);
            _hidSysMultiDept.Value = (string.IsNullOrEmpty(multiDept)) ? "0" : multiDept; //系统启用多部门多职位功能开关
        }


        private void initEdit(MsUser msUser, eEffect ukEffect, eEffect multiDeptEffect)
        {
            if (null == msUser) return;
                      
            txtUserName.Text = msUser.userName;
            txtUserCode.Text = msUser.userCode;
            txtLgName.Text = msUser.loginName;

            string classId = msUser.belongClass.clsId;
            _hidSinClsId.Value = classId;
            ddlClass.SelectedValue = classId;

            if (ukEffect == eEffect.YES)
            {
                rdlist.SelectedValue = msUser.ePassSign.ToString("D");
                txtUseKey.Text = msUser.ePass;
            }

            if (multiDeptEffect == eEffect.NO)
            {
                string deptId = msUser.belongDept.deptId;
                string posId = msUser.belongPos.posId;

                ddlDept.SelectedValue = deptId;
                ddlPos.SelectedValue = posId;
                _hidSinDeptId.Value = deptId;
                _hidSinPosId.Value = posId;
            }
            else
            {
                #region 暂时不作处理 tim 2010-02-23
                /* 此处等待部门、级别多选控件 */
                //_hidDeptId.Value = msUser.belong.DeptId;
                //txtDeptName.Text = oUser.belong.DeptName;

                //_hidPosId.Value = oUser.belong.PosId;
                //txtPosName.Text = oUser.belong.PosName;
                #endregion
            }

        }


        protected void lnbCallBack_Click(object sender, EventArgs e)
        {
            initializePage();
        }

        #region ajax methods

        /// <summary>
        /// 登陆名同名校验
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="loginName">登陆名</param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string CheckLoginNameExist(string userId, string loginName)
        {
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = userBLL.IsExistLoginName(loginName, userId);
            var newObj = new {
                OPERTYPE = "LGNAMECHECK",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1]
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            lstResult = null;
            newObj = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
        }

        /// <summary>
        /// 保存用户信息
        /// </summary>
        /// <param name="jsonExpr">用户信息表达式</param>
        /// <param name="ctrlType"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string SaveUserData(string jsonExpr,string ctrlType)
        {
            MsUser msUser = JavaScriptConvert.DeserializeObject(jsonExpr,typeof(MsUser)) as MsUser;
            UserBLL userBLL = new UserBLL();
            List<string> lstResult = userBLL.Save(msUser);
            var newObj = new {
                OPERTYPE = "USERSAVE",
                RSTSIGN = lstResult[0],
                RSTMSG = lstResult[1],
                CTRLTYPE = ctrlType
            };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);

            lstResult = null;
            newObj = null;
            msUser = null;
            userBLL.Release();
            userBLL = null;
            return serExpr;
            #region logout tim 2010-02-23
            //XmlDocument oDom = new XmlDocument();
            //oDom.LoadXml(strXML);

            ///* 按钮的类型
            // * 1: 保存按钮点击 2: 保存新增按钮 3: 保存返回按钮
            // * */
            //string sCtrlType = oDom.GetElementsByTagName("CTRLTYPE")[0].InnerText;
            //string strUserXML = oDom.GetElementsByTagName("USER")[0].OuterXml;
            //oDom = null;

            //UserNET oUser = new UserNET(strUserXML);
            //StringBuilder sb = new StringBuilder("<RESULT>");
            //const string cnsOperType = "USERSAVE";
            //string sUserId = "";
            //bool bExe = false;

            //if (oUser.UserId == "")
            //    sUserId = oUser.AddNew();
            //else
            //{
            //    bExe = oUser.Update();
            //    sUserId = (bExe) ? oUser.UserId : "";
            //}

            //sb.Append("<CTRLTYPE>").Append(sCtrlType).Append("</CTRLTYPE>");
            //sb.Append("<OPERTYPE>").Append(cnsOperType).Append("</OPERTYPE>");
            //sb.Append("<EXERESULT>").Append(sUserId).Append("</EXERESULT>");
            //sb.Append("</RESULT>");

            //oUser = null;
            //return sb.ToString();
            #endregion
        }

        #endregion
    }
}
